import 'package:flui/flui.dart';
import 'package:flutter/material.dart';
import 'package:fzcgx_app/common/adapter_utils.dart';
import 'package:fzcgx_app/common/assets_utils.dart';
import 'package:fzcgx_app/provider/provider_widget.dart';
import 'package:fzcgx_app/ui/base/base_view_model.dart';
import 'package:fzcgx_app/ui/order/model/order_release_model.dart';
import 'package:fzcgx_app/ui/order/page/order_release_details_page.dart';
import 'package:fzcgx_app/ui/order/view_model/order_release_view_model.dart';
import 'package:nav_router/nav_router.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class OrderReleasePage extends StatefulWidget {
  OrderReleasePage({Key key}) : super(key: key);

  @override
  _OrderReleasePageState createState() => _OrderReleasePageState();
}

class _OrderReleasePageState extends State<OrderReleasePage>
    with TickerProviderStateMixin, AutomaticKeepAliveClientMixin {
  TabController _tabController;
  List _titleList = [
    '普通单',
    '抢单',
  ];
  @protected
  bool get wantKeepAlive => true;
  @override
  void initState() {
    super.initState();
    _tabController = null;
    _tabController = TabController(
        initialIndex: 0, length: _titleList.length, vsync: this); // 直接传this

    print("---->${_tabController.previousIndex}");

    if (_tabController.indexIsChanging) {
      print("---->indexch");
    }
  }

  @override
  // ignore: must_call_super
  Widget build(BuildContext context) {
    return ProviderWidget<OrderReleaseViewModel>(
      model: OrderReleaseViewModel(),
      onReady: (viewModel) {
        viewModel.requestApi();
      },
      builder: (context, viewModel, child) {
        return viewModel.state == ViewState.Success
            ? Scaffold(
                backgroundColor: Color(0xffF5F5F5),
                body: SafeArea(
                    child: Column(
                  children: <Widget>[
                    SizedBox(height: AdapterUtils.getHeight(10)),
                    Container(
                      alignment: Alignment.centerLeft,
                      child: TabBar(
                        controller: _tabController,
                        isScrollable: true,
                        indicatorColor: Color(0xff15A1FF),
                        indicatorWeight: 2,
                        indicatorSize: TabBarIndicatorSize.label,
                        labelStyle: TextStyle(
                            color: Color(0xff15A1FF),
                            fontWeight: FontWeight.w500,
                            fontSize: AdapterUtils.getSp(14)),
                        labelColor: Color(0xff15A1FF),
                        unselectedLabelColor: Color(0xff3B4859),
                        unselectedLabelStyle: TextStyle(
                            color: Color(0xff3B4859),
                            fontWeight: FontWeight.w500,
                            fontSize: AdapterUtils.getSp(14)),
                        tabs: _titleList.map((f) {
                          return Tab(
                            text: f,
                          );
                        }).toList(),
                      ),
                    ),
                    Expanded(
                        child: SmartRefresher(
                      controller: viewModel.refreshController,
                      enablePullDown: true,
                      enablePullUp: true,
                      onRefresh: viewModel.onRefresh,
                      onLoading: viewModel.onLoading,
                      header: WaterDropHeader(
                        waterDropColor: Color(0xff15A1FF),
                      ),
                      child: ListView.builder(
                        itemCount: viewModel.models.length,
                        itemBuilder: (BuildContext context, int index) {
                          return Container(
                            margin: EdgeInsets.only(
                                bottom: AdapterUtils.getHeight(10),
                                top: index == 0
                                    ? AdapterUtils.getHeight(10)
                                    : 0),
                            child: _buildOrderItemWidget(
                                viewModel, viewModel.models[index]),
                          );
                        },
                      ),
                    )),
                  ],
                )),
              )
            : Container();
      },
    );
  }

  Widget _buildOrderItemWidget(
      OrderReleaseViewModel viewModel, OrderReleaseModel model) {
    return InkWell(
      onTap: () {
        routePush(OrderReleaseDetailsPage());
      },
      child: Container(
        width: AdapterUtils.screenWidth(),
        margin: EdgeInsets.only(
          left: AdapterUtils.getWidth(15),
          right: AdapterUtils.getWidth(15),
        ),
        padding: EdgeInsets.all(AdapterUtils.getWidth(12)),
        decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.all(Radius.circular(12))),
        child: Column(
          children: <Widget>[
            Container(
              child: Row(
                children: <Widget>[
                  FLAvatar(
                    image: Image.network(model.uhead),
                    width: AdapterUtils.getWidth(22),
                    height: AdapterUtils.getHeight(22),
                  ),
                  SizedBox(width: AdapterUtils.getWidth(6)),
                  Text(model.comName,
                      style: TextStyle(
                          color: Color(0xff3B4859),
                          fontWeight: FontWeight.w600,
                          fontSize: AdapterUtils.getSp(16))),
                  SizedBox(
                    width: AdapterUtils.getWidth(10),
                  ),
                  Image.asset(
                    AssetsUtils.getAssetsImagesPath('home_arrow_right.png'),
                  ),
                  SizedBox(
                    width: AdapterUtils.getWidth(10),
                  ),
                  Container(
                    constraints: BoxConstraints.expand(
                      height: AdapterUtils.getHeight(16),
                      width: AdapterUtils.getWidth(36),
                    ),
                    decoration: BoxDecoration(
                      image: DecorationImage(
                        image: AssetImage(AssetsUtils.getAssetsImagesPath(
                          'tag_blue.png',
                        )),
                        //这里是从assets静态文件中获取的，也可以new NetworkImage(）从网络上获取
                        centerSlice: Rect.fromLTRB(270.0, 180.0, 1360.0, 730.0),
                      ),
                    ),
                    alignment: Alignment.center,
                    child: Text('普通',
                        textAlign: TextAlign.center,
                        style: TextStyle(
                            color: Colors.white,
                            fontWeight: FontWeight.w500,
                            fontSize: AdapterUtils.getSp(8))),
                  ),
                  Spacer(),
                  Text(model.statusStr,
                      style: TextStyle(
                          color: Color(0xff15A1FF),
                          fontWeight: FontWeight.w500,
                          fontSize: AdapterUtils.getSp(13))),
                ],
              ),
            ),
            SizedBox(height: AdapterUtils.getHeight(15)),
            Container(
              child: Row(
                children: <Widget>[
                  Image.network(
                    model.img,
                    width: AdapterUtils.getWidth(90),
                    height: AdapterUtils.getHeight(90),
                    fit: BoxFit.cover,
                  ),
                  SizedBox(
                    width: AdapterUtils.getWidth(15),
                  ),
                  Expanded(
                      child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Text(model.proName,
                          style: TextStyle(
                              color: Color(0xff303030),
                              fontWeight: FontWeight.w600,
                              fontSize: AdapterUtils.getSp(14))),
                      SizedBox(height: AdapterUtils.getHeight(4)),
                      Row(
                        children: <Widget>[
                          Text(model.proStyle,
                              style: TextStyle(
                                  color: Color(0xffA0A0A0),
                                  fontWeight: FontWeight.w500,
                                  fontSize: AdapterUtils.getSp(12))),
                          Spacer(),
                          Text('x${model.quantity}',
                              style: TextStyle(
                                  color: Color(0xffA0A0A0),
                                  fontWeight: FontWeight.w500,
                                  fontSize: AdapterUtils.getSp(12))),
                        ],
                      ),
                      SizedBox(height: AdapterUtils.getHeight(8)),
                      RichText(
                        text: TextSpan(children: <InlineSpan>[
                          TextSpan(
                            text: '发布方电话：',
                            style: TextStyle(
                              fontSize: AdapterUtils.getSp(12),
                              color: Color(0xff606B79),
                              fontWeight: FontWeight.w500,
                            ),
                          ),
                          TextSpan(
                            text: model.phone,
                            style: TextStyle(
                              fontSize: AdapterUtils.getSp(12),
                              color: Color(0xffFF3D3E),
                              fontWeight: FontWeight.w500,
                            ),
                          ),
                        ]),
                      ),
                    ],
                  ))
                ],
              ),
            ),
            SizedBox(
              height: AdapterUtils.getHeight(17),
            ),
            Container(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: <Widget>[
                  RichText(
                    text: TextSpan(children: <InlineSpan>[
                      TextSpan(
                        text: '单价',
                        style: TextStyle(
                          fontSize: AdapterUtils.getSp(14),
                          color: Color(0xff99A2AD),
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                      TextSpan(
                        text: '￥',
                        style: TextStyle(
                          fontSize: AdapterUtils.getSp(10),
                          color: Color(0xff99A2AD),
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                      TextSpan(
                        text: model.price,
                        style: TextStyle(
                          fontSize: AdapterUtils.getSp(14),
                          color: Color(0xff99A2AD),
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ]),
                  ),
                  SizedBox(
                    width: AdapterUtils.getWidth(10),
                  ),
                  RichText(
                    text: TextSpan(children: <InlineSpan>[
                      TextSpan(
                        text: '实付款',
                        style: TextStyle(
                          fontSize: AdapterUtils.getSp(14),
                          color: Color(0xff141F33),
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                      TextSpan(
                        text: '￥',
                        style: TextStyle(
                          fontSize: AdapterUtils.getSp(10),
                          color: Color(0xff141F33),
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                      TextSpan(
                        text: model.totalPrice.toString(),
                        style: TextStyle(
                          fontSize: AdapterUtils.getSp(14),
                          color: Color(0xff141F33),
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ]),
                  ),
                ],
              ),
            ),
            SizedBox(
              height: AdapterUtils.getHeight(17),
            ),
            Container(
              child: Row(
                children: <Widget>[
                  Expanded(
                      child: InkWell(
                    child: Container(
                      height: AdapterUtils.getHeight(40),
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                          color: Colors.white,
                          border:
                              Border.all(color: Color(0xffD7D8DB), width: 1),
                          borderRadius: BorderRadius.all(Radius.circular(15))),
                      child: Text('查看详情',
                          style: TextStyle(
                              color: Color(0xff606B79),
                              fontWeight: FontWeight.w500,
                              fontSize: AdapterUtils.getSp(12))),
                    ),
                  ))
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}
